﻿Ext.define("PSI.InPlan.SendTruckMainForm", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({ text: "派车", iconCls: "psi-button-transmit_blue", handler: me.onSendTruck, scope: me });

        result.push("-");
        result.push({ text: "取消派车", iconCls: "psi-button-cancel", handler: me.onCancelTruck, scope: me });
        result.push("-");
        result.push({ text: "更改调度", iconCls: "psi-button-user_edit", handler: me.onChangeDispatcher, scope: me });
        result.push("-");
        result.push({ text: "刷新", iconCls: "psi-button-refresh", handler: me.onRefreshPlanGird, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function (dispatcherName) {
        var me = this;
        me.dispatcherName = dispatcherName;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "进口计划派车",
            tbar: me.createToolButtons(),
            items: [{
                region: "north", xtype: "container",
                height: 70,
                layout: {
                    type: "table",
                    columns: 8
                }, border: false,
                items: [{
                    xtype: "label",
                    text: "调度"
                }, {
                    xtype: "psi_bc_dispatcherfield",
                    margin: "4 5 0 5",
                    id: "editDispatcherQC",
                    value: dispatcherName,
                    width: 170
                }, {
                    xtype: "label",
                    text: "拖车公司"
                }, {
                    xtype: "psi_truck_truckcompanyfield",
                    margin: "4 5 0 5",
                    id: "editTruckCompanyQC",
                    value:"",
                    width: 170
                }, {
                    xtype: "label",
                    text: "车号"
                }, {
                    xtype: "psi_truck_trucknumberfield",
                    id: "editTruckNumberQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "司机"
                }, {
                    xtype: "textfield",
                    id: "editDriverQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "司机电话"
                }, {
                    xtype: "textfield",
                    id: "editDriverTelQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "container",
                    border: 0,
                    layout: "hbox",
                    items: [
                        {
                            xtype: "button",
                            text: "查询",
                            iconCls: "psi-button-refresh",
                            margin: "4 5 0 5",
                            width: 90,
                            handler: me.onRefreshPlanGird,
                            scope: me
                        }, {
                            xtype: "button",
                            text: "清空查询条件",
                            margin: "4 5 0 5",
                            handler: me.onClearQC,
                            scope: me
                        }
                    ],
                    colspan: 2
                }
                ]
            },
                {
                    region: "center", xtype: "container", layout: "fit", border: false,
                    items: [me.getInPlanGrid()]
                }, {
                    region: "south", xtype: "container", border: false, height: 30,
                    items: [
                        {
                            xtype: "displayfield",
                            id: "editPlanInfo",
                            value: ""
                        }
                    ]
                }
            ]
        });

        me.panel = panel;

        Ext.getCmp("editTruckNumberQC").setLinkedEditor({
            truckCompanyEditor: Ext.getCmp("editTruckCompanyQC")
        });

        me.refreshInPlanGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getInPlanGrid: function () {
        var me = this;

        if (!me.__inPlanGrid) {
            me.__inPlanGrid = me.createInPlanGrid();
        }

        return me.__inPlanGrid;
    },

    createInPlanGrid: function () {
        var modelName = "PSIInPlanModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "address", "cntNumber", "cntType", "deliverDT", "memo",
                "planDT", "dispatcher", "truckDT", "truckCompany", "truckNumber",
                "driver", "driverTel", "dt1Plan", "dt2Plan",
                "dt3Plan", "dt5Plan" , "dt4Plan", "vsl", "clients", "cntOwner",
                "unloadMan", "unloadManTel", "tripNumber", "status"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "状态", dataIndex: "status", menuDisabled: true },
                    { header: "趟次号", dataIndex: "tripNumber", menuDisabled: true},
                    { header: "进口计划号", dataIndex: "planNumber", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "送货时限", dataIndex: "deliverDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true },
                    { header: "计划下达时间", dataIndex: "planDT", menuDisabled: true, width: 120 },
                    { header: "调度", dataIndex: "dispatcher", menuDisabled: true },
                    { header: "拖车公司", dataIndex: "truckCompany", menuDisabled: true },
                    { header: "车号", dataIndex: "truckNumber", menuDisabled: true },
                    { header: "司机", dataIndex: "driver", menuDisabled: true },
                    { header: "司机电话", dataIndex: "driverTel", menuDisabled: true },
                    { header: "派车时间", dataIndex: "truckDT", menuDisabled: true, width: 120 },
                    { header: "卸货联系人", dataIndex: "unloadMan", menuDisabled: true },
                    { header: "卸货人电话", dataIndex: "unloadManTel", menuDisabled: true },
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "计划提箱时间", dataIndex: "dt1Plan", menuDisabled: true, width: 120 },
                    { header: "计划出港时间", dataIndex: "dt2Plan", menuDisabled: true, width: 120 },
                    { header: "计划到卸货地时间", dataIndex: "dt3Plan", menuDisabled: true, width: 120 },
                    { header: "计划卸货开始时间", dataIndex: "dt5Plan", menuDisabled: true, width: 120 },
                    { header: "计划卸货完成时间", dataIndex: "dt4Plan", menuDisabled: true, width: 120 }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    onRefreshPlanGird: function () {
        this.refreshInPlanGrid();
    },

    refreshInPlanGrid: function (selectedId) {
        var me = this;
        var grid = me.getInPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }

         
        var dispatcher = Ext.getCmp("editDispatcherQC").getValue()
        var truckCompany = Ext.getCmp("editTruckCompanyQC").getValue()
        var truckNumber= Ext.getCmp("editTruckNumberQC").getValue()
        var driver=Ext.getCmp("editDriverQC").getValue()
        var driverTel= Ext.getCmp("editDriverTelQC").getValue()

        var params = {
            dispatcher: dispatcher,
            truckCompany: truckCompany,
            truckNumber: truckNumber,
            driver: driver,
            driverTel: driverTel
        };

        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetInPlanSendTruckListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                    var info = "进口计划共计: <span style='color:red'>" + data.length + "</span>个";
                    Ext.getCmp("editPlanInfo").setValue(info);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onMakePlan: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            PSI.MsgBox.showInfo("请选择要下达的计划");
            return;
        }

        // 检查该计划是否已经下达过了
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            if (obj.planDT) {
                var info = "计划号为[" + obj.planNumber + "]的进口计划已经下达";
                PSI.MsgBox.showInfo(info);
                return;
            }
        }

        var idList = [];
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            idList.push(obj.id);
        }

        var form = Ext.create("PSI.InPlan.MakePlanForm");
        form.show(me, { idList: Ext.JSON.encode(idList) });
    },

    onChangeDispatcher: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            PSI.MsgBox.showInfo("请选择要更改调度的计划");
            return;
        }

        // 检查计划是否都已经下达
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            if (!obj.planDT) {
                var info = "计划没有下达，不能更改调度";
                PSI.MsgBox.showInfo(info);
                return;
            }

            if (obj.dispatcher != me.dispatcherName) {
                var info = "当前计划的调度是<span style='color:red'>" + obj.dispatcher + "</span>"
                    + ", 您没有权限替别人更改调度";
                PSI.MsgBox.showInfo(info);
                return;
            }

        }

        var idList = [];
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            idList.push(obj.id);
        }

        var form = Ext.create("PSI.InPlan.ChangeDispatcherForm");
        form.show(me, { idList: Ext.JSON.encode(idList) });
    },

    onSendTruck: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要一个要派车的进口计划");
            return;
        }
        var plan = items[0].getData();

        if (!plan.planDT) {
            PSI.MsgBox.showInfo("当前计划还没有下达，不能派车");
            return;
        }

        if (plan.dispatcher != me.dispatcherName) {
            var info = "当前计划的调度是<span style='color:red'>" + plan.dispatcher + "</span>"
                + ", 您没有权限替别人派车";
            PSI.MsgBox.showInfo(info);
            return;
        }

        var form = Ext.create("PSI.InPlan.SendTruckForm");
        form.show(me, plan);
    },

    onCancelTruck: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要一个要取消派车的进口计划");
            return;
        }
        var plan = items[0].getData();

        if (!plan.truckDT) {
            PSI.MsgBox.showInfo("当前计划还没有派车，无需取消派车");
            return;
        }

        var info = "请确认是否要取消计划[<span style='color:red'>" + plan.planNumber + "</span>]的派车?";
        PSI.MsgBox.confirm(info, function () {
            var el = Ext.getBody();
            el.mask("正在处理中...");
            Ext.Ajax.request({
                url: "/InPlan/CancelTruck",
                method: "POST",
                params: { id: plan.id },
                callback: function (options, success, response) {
                    el.unmask();

                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功取消派车", function () {
                                me.refreshInPlanGrid();
                            });
                        } else {
                            PSI.MsgBox.showInfo(data.msg);
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误");
                    }
                }
            });
        });
    },

    onTrace: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个进口计划");
            return;
        }
        var plan = items[0].getData();
        if (!plan.truckDT) {
            PSI.MsgBox.showInfo("当前计划还没有派车，无法录入动态信息");
            return;
        }

        var form = Ext.create("PSI.InPlan.TraceForm");
        form.show(me, plan);
    },

    onClearQC: function () {
        Ext.getCmp("editDispatcherQC").setValue(null);
        Ext.getCmp("editTruckCompanyQC").setValue(null);
        Ext.getCmp("editTruckNumberQC").setValue(null);
        Ext.getCmp("editDriverQC").setValue(null);
        Ext.getCmp("editDriverTelQC").setValue(null);
    }
});